﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Collections;
using System.Threading;
using System.Media;

namespace HRMChat
{
    public partial class frmMain : DevExpress.XtraEditors.XtraForm
    {
        ManualResetEvent done = new ManualResetEvent(false);
        frmChat[] listChat = new frmChat[500];
        int chatCount = -1;
        Hashtable chatIndex = new Hashtable();
        public frmMain()
        {
            InitializeComponent();
        }

        public frmLogin frmLogin
        {
            get;
            set;
        }

        public void rosterManager_OnRosterItem(object sender, jabber.protocol.iq.Item ri)
        {
            try
            {
                chatIndex.Add(ri.JID.Bare, ++chatCount);
                InitFrmChat(ri.JID.Bare, ri.Nickname);
            }
            catch (Exception)
            {
            }
        }

        public void rosterManager_OnRosterEnd(object sender)
        {
            done.Set();
            rosterTree.EndUpdate();
            Program.jbClient.Presence(jabber.protocol.client.PresenceType.available, txtStatus.Text, null, 0);
            lblPresence.Text = "Available";
            rosterTree.ExpandAll();
        }

        public void rosterManager_OnRosterBegin(object sender)
        {
            listChat = new frmChat[500];
            chatIndex = new Hashtable();
            rosterTree.BeginUpdate();
        }

        public void jbClient_OnMessage(object sender, jabber.protocol.client.Message msg)
        {
            listChat[(int)chatIndex[msg.From.Bare]].ReceiveFlag = true;
            listChat[(int)chatIndex[msg.From.Bare]].Buzz(msg.Body);

            if (string.IsNullOrEmpty(msg.Body) || msg.Body == " ")
            {
                listChat[(int)chatIndex[msg.From.Bare]].AppendConversation(msg.Body);
            }
            else
            {
                string receiveMsg = msg.From.User + " nói: " + msg.Body + "\n";
                listChat[(int)chatIndex[msg.From.Bare]].AppendConversation(receiveMsg);
                listChat[(int)chatIndex[msg.From.Bare]].Show();
                listChat[(int)chatIndex[msg.From.Bare]].Activate();
            }
        }

        public void jbClient_OnError(object sender, Exception ex)
        {
            MessageBox.Show(ex.Message);
            this.frmLogin.Show();
            this.Hide();
        }

        public void jbClient_OnDisconnect(object sender)
        {
            Program.jbClient = new jabber.client.JabberClient(this.components);

            Program.jbClient.AutoReconnect = 30F;
            Program.jbClient.AutoStartCompression = true;
            Program.jbClient.AutoStartTLS = true;
            Program.jbClient.InvokeControl = this;
            Program.jbClient.KeepAlive = 30F;
            Program.jbClient.LocalCertificate = null;
            Program.jbClient.Password = null;
            Program.jbClient.Server = "gmail.com";
            Program.jbClient.User = null;

            Program.jbClient.OnAuthenticate += this.frmLogin.jbClient_OnAuthenticate;
            Program.jbClient.OnAuthError += this.frmLogin.jbClient_OnAuthError;
            Program.jbClient.OnError += this.frmLogin.jbClient_OnError;

            this.frmLogin.Show();
            this.Hide();
        }

        private void lblStatus_MouseClick(object sender, MouseEventArgs e)
        {
            lblStatus.Visible = false;
            txtStatus.Visible = true;
        }

        private void txtStatus_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                ChangeStatus();
            }
        }

        private void ChangeStatus()
        {
            if (!string.IsNullOrEmpty(txtStatus.Text))
            {
                lblStatus.Text = txtStatus.Text;
                lblStatus.Visible = true;
                txtStatus.Visible = false;
                Program.jbClient.Presence(jabber.protocol.client.PresenceType.available, txtStatus.Text, null, 0);
            }
        }

        private void InitFrmChat(string targetId, string nickName)
        {
            listChat[chatCount] = new frmChat();
            listChat[chatCount].MailId = targetId;
            listChat[chatCount].jbClient = Program.jbClient;
            if (nickName != null)
                listChat[chatCount].Text = nickName;
            else
                listChat[chatCount].Text = (targetId.Split('@'))[0];
            listChat[chatCount].jbClient.OnMessage += new jabber.client.MessageHandler(listChat[chatCount].jbClient_OnMessage);
        }

        private void btnSignOut_Click(object sender, EventArgs e)
        {
            Program.jbClient.Close(true);
        }

        public void rosterTree_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            muzzle.RosterTree.ItemNode selectedNode = rosterTree.SelectedNode as muzzle.RosterTree.ItemNode;
            if (selectedNode != null)
                listChat[(int)chatIndex[selectedNode.JID.Bare]].Show();
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            Program.jbClient.Close(true);
        }

        private void rosterTree_Click(object sender, EventArgs e)
        {
            ChangeStatus();
        }

        private void frmMain_Click(object sender, EventArgs e)
        {
            ChangeStatus();
        }

    }
}